package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Modules;

public class GenericModuleDAO extends AutoKeys implements
		org.jetnuke.dao.ModuleDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericModuleDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ModuleModel#retrieveByPK(int)
	 */
	public Modules retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("ModuleModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		Modules s = new Modules();

		if (rs.next()) {
			fillModuleFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillModuleFromResultSet(Modules s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("fillModuleFromResultSet(Modules, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setACTIVE(rs.getInt("ACTIVE"));
		s.setBKEY(rs.getString("BKEY"));
		s.setINMENU(rs.getInt("INMENU"));
		s.setMID(rs.getInt("MID"));
		s.setPATH(rs.getString("PATH"));
		s.setTITLE(rs.getString("TITLE"));
		s.setTITLE_I18N(rs.getString("TITLE_I18N"));

		if (logger.isDebugEnabled()) {
			logger.debug("fillModuleFromResultSet(Modules, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Modules u = new Modules();
			fillModuleFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("ModuleModel.selectAll"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ModuleModel#update(org.jetnuke.entities.Modules)
	 */
	public void update(Modules module) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Modules) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("ModuleModel.update"));
		p.setInt(1, module.getACTIVE());
		p.setString(2, module.getBKEY());
		p.setInt(3, module.getINMENU());
		p.setString(4, module.getPATH());
		p.setString(5, module.getTITLE());
		p.setString(6, module.getTITLE_I18N());
		p.setInt(7, module.getMID());

		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Modules) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ModuleModel#selectAllActiveInMenu()
	 */
	public List selectAllActiveInMenu() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActiveInMenu() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("ModuleModel.selectAllActiveInMenu"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActiveInMenu() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ModuleModel#selectAllInactive()
	 */
	public List selectAllInactive() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllInactive() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("ModuleModel.selectAllInactive"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllInactive() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ModuleModel#selectAllActiveNotMenu()
	 */
	public List selectAllActiveNotMenu() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActiveNotMenu() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("ModuleModel.selectAllActiveNotMenu"));

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllActiveNotMenu() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ModuleModel#deleteByPK(int)
	 */
	public void deleteByPK(int mid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("ModuleModel.deleteByPK"));
		p.setInt(1, mid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByPK(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ModuleModel#addNew(org.jetnuke.entities.Modules)
	 */
	public int addNew(Modules module) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Modules) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("ModuleModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("ModuleModel.lastGeneratedModuleID"));
		initNewModule(module, p);

		int id = this.executeAutoKeysQuery(p);

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Modules) - end"); //$NON-NLS-1$
		}
		return id;

	}

	/**
	 * @param module
	 * @param p
	 */
	private void initNewModule(Modules module, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewModule(Modules, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, module.getACTIVE());
		p.setString(2, module.getBKEY());
		p.setInt(3, module.getINMENU());
		p.setString(4, module.getPATH());
		p.setString(5, module.getTITLE());
		p.setString(6, module.getTITLE_I18N());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewModule(Modules, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

}
